package org.diffgenova;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filter.FilterListener;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ListRifiutiActivity extends ListActivity {
    Map<String, String> rifiuti = new HashMap<String, String>();
    private DataBaseHelper dbHelper;
    private SimpleCursorAdapter adapter;
    private AutoCompleteTextView filterText = null;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        
        ListView lv;
        lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.requestFocus();
        filterText = (AutoCompleteTextView) findViewById(R.id.search_box);
        filterText.addTextChangedListener(filterTextWatcher);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Cursor c = (Cursor) l.getItemAtPosition(position);
        String rifiuto = c.getString(0);
        String categoria = c.getString(2);
        Log.e("rifiuto", "Rifiuto: " + rifiuto);
        Log.e("rifiuto", "Categoria: " + categoria);
        //Toast.makeText(getApplicationContext(), categoria, Toast.LENGTH_SHORT).show();
        
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra("rifiuto", rifiuto);
        intent.putExtra("categoria", categoria);
        startActivity(intent);
        
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        dbHelper = new DataBaseHelper(getApplicationContext());
        try {
            dbHelper.createDataBase();
            dbHelper.openDataBase();
            Cursor rifiuti = dbHelper.getRifiuti();
            String[] columns = {
                    "rifiutodes"
            }; // , "categoriades" };
            int[] textIds = {
                    R.id.rifiuto
            }; // , R.layout.list_item };
            adapter = new ListRifiutiAdapter(getApplicationContext(),
                    R.layout.list_item, rifiuti, columns, textIds);
            adapter.setFilterQueryProvider(new TextQuery());
            filterListener = new FilterListener() {
                
                public void onFilterComplete(int count) {
                    adapter.notifyDataSetChanged();
                }
            };
            Editable filterString = filterText.getText();
			if(filterString.length() > 0){
                Filter filter = adapter.getFilter();
    			filter.filter(filterString, filterListener);
            }
            setListAdapter(adapter);
            // filterText.setAdapter(adapter);
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.getClass().getName()
                    + ": " + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    
    @Override
    protected void onPause() {
        dbHelper.close();
        super.onPause();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EditText et = (EditText) findViewById(R.id.search_box);
            String searchValue = et.getText().toString();
            if (searchValue.length() > 0) {
                et.setText("");
                return true;
            } else
                return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }
    
    FilterListener filterListener;

    private TextWatcher filterTextWatcher = new TextWatcher() {
        
        public void afterTextChanged(Editable s) {
        }
        
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Filter filter = adapter.getFilter();
			filter.filter(s, filterListener);
        }
        
    };
    
    class TextQuery implements FilterQueryProvider {
        public Cursor runQuery(CharSequence arg0) {
            // .... build a new select and provide a cursor
            // dbHelper.openDataBase();
            Cursor rifiuti = dbHelper.getRifiuti(arg0.toString());
            // dbHelper.close();
            return rifiuti;
        }
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.legenda:
                // showDialog(R.layout.legenda);
                Intent intent = new Intent(this, ListCategorieActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch (id) {
            case R.layout.legenda:
                dialog = new Dialog(this);
                
                dialog.setContentView(R.layout.legenda);
                dialog.setTitle("Legenda");
                ListView listLegendView = (ListView) dialog.findViewById(R.id.list_legenda);
                
                Cursor categorie = dbHelper.getCategorie();
                String[] columns = {
                        "categoriades"
                }; // , "categoriades" };
                int[] textIds = {
                        R.id.rifiuto
                }; // , R.layout.list_item };
                adapter = new ListCategorieAdapter(getApplicationContext(),
                        R.layout.list_item, categorie, columns, textIds);
                adapter.setFilterQueryProvider(new TextQuery());
                listLegendView.setAdapter(adapter);
                break;
            default:
                dialog = null;
        }
        return dialog;
    }
}
